package com.lg.template.controller;

import com.lg.template.mapper.mybatis.UserMapper;
import com.lg.template.entity.User;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Api(tags = "Mybatis测试")
@RestController
@RequestMapping("/mybatis")
public class MybatisController {
    @Autowired
    private UserMapper userMapper;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("queryAllUser")
    public   List<User>  queryAllUser(@RequestBody User u){
        List<User> users = userMapper.queryAllUser(u);
        for (User user : users) {
            System.out.println(user);
        }
        return users;
    }

    @GetMapping("queryUser")
    public User queryUser(){
        User user = userMapper.queryUser();
        System.out.println(user);
        return user;
    }

    @GetMapping("queryByMap")
    public User queryByMap(){
        HashMap<String, Object> parameter = new HashMap<>();
        parameter.put("userId", 8);
        parameter.put("userPwd", "root");
        User user = userMapper.queryByMap(parameter);
        System.out.println(user);
        return user;
    }

    @PostMapping("queryById")
    public User queryById(@RequestBody User u){

        User user = userMapper.queryById(u);
        System.out.println(user);
        return user;
    }

    @GetMapping("randomInsert")
    public Integer randomInsert(){
        User user = new User();
        user.setName("randomName");
        user.setPwd("root");
        user.setHeadImg(new byte[]{1,2,3});
        user.setCreateTime(new Date());

        Integer res = userMapper.randomInsert(user);
        System.out.println(user);
        return res;
    }



    @GetMapping("updateByIdAndName")
    public Integer updateByIdAndName(Integer id){
        User user = new User();
        user.setPwd("new pwd");
        user.setId(id);

        Integer res = userMapper.updateById(user);
        System.out.println(user);
        return res;
    }

    @GetMapping("deleteById")
    public Integer deleteById(Integer id,String name){
        Integer res = userMapper.deleteById(id,name);
        return res;
    }









    @GetMapping("/baseConfig")
    public String getBaseConfig(){
        return serverPort;
    }


}
